package org.meijiframework.gradle.plugins;

import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.Convention;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.ProjectPluginsContainer;
import org.gradle.api.tasks.ConventionValue;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.internal.IConventionAware;
import org.meijiframework.gradle.tasks.SchemaExportTask;

public class HibernateToolsPlugin implements Plugin {
	
	public static final String SCHEMA_EXPORT = "schemaExport";

	@Override
	public void use(Project project, ProjectPluginsContainer container) {
		System.out.println("Using Hibernate Tools Plugin");
        container.usePlugin(JavaPlugin.class, project);

        HibernateToolsConvention hibernateToolsConvention = new HibernateToolsConvention();
        Convention convention = project.getConvention();
        convention.getPlugins().put("hibernateTools", hibernateToolsConvention);
        
        configureSchemaExport(project, hibernateToolsConvention);
	}
	
    private void configureSchemaExport(final Project project, final HibernateToolsConvention hibernateToolsConvention) {
        project.getTasks().withType(SchemaExportTask.class).whenTaskAdded(new Action<SchemaExportTask>() {
            public void execute(SchemaExportTask schemaExport) {
                schemaExport.getConventionMapping().map("classpath", new ConventionValue() {
                    public Object getValue(Convention convention, IConventionAware conventionAwareObject) {
                        return getJavaConvention(project).getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath();
                    }
                });
                schemaExport.getConventionMapping().map("format", new ConventionValue() {
                    public Object getValue(Convention convention, IConventionAware conventionAwareObject) {
                        return hibernateToolsConvention.getFormat();
                    }
                });
                schemaExport.getConventionMapping().map("comments", new ConventionValue() {
                    public Object getValue(Convention convention, IConventionAware conventionAwareObject) {
                        return hibernateToolsConvention.getComments();
                    }
                });
                schemaExport.getConventionMapping().map("dialects", new ConventionValue() {
                    public Object getValue(Convention convention, IConventionAware conventionAwareObject) {
                        return hibernateToolsConvention.getDialects();
                    }
                });
                schemaExport.getConventionMapping().map("persistenceUnitName", new ConventionValue() {
                    public Object getValue(Convention convention, IConventionAware conventionAwareObject) {
                        return hibernateToolsConvention.getPersistenceUnitName();
                    }
                });
                schemaExport.getConventionMapping().map("delimiter", new ConventionValue() {
                    public Object getValue(Convention convention, IConventionAware conventionAwareObject) {
                        return hibernateToolsConvention.getDelimiter();
                    }
                });
                schemaExport.getConventionMapping().map("targetDirectory", new ConventionValue() {
                    public Object getValue(Convention convention, IConventionAware conventionAwareObject) {
                        return hibernateToolsConvention.getTargetDirectory();
                    }
                });
                
            }
        });

        SchemaExportTask schemaExport = project.getTasks().add(SCHEMA_EXPORT, SchemaExportTask.class);
        schemaExport.setDescription("Generates DDL scripts for your database from your JPA persistent classes.");
    }
	
    public JavaPluginConvention getJavaConvention(Project project) {
        return project.getConvention().getPlugin(JavaPluginConvention.class);
    }
}
